package test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * in testing
 *
 * @author user192
 */
public class UniqueValidator
        implements ConstraintValidator<Unique, String> {

    private String[] fields;

    @Override
    public void initialize(Unique annotation) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        System.err.println("annotationType.getName(): " + annotationType.getName());
        Method[] methods = annotationType.getMethods();

        for (int i = 0; i < methods.length; i++) {
            String name = methods[i].getName();
            System.err.println("methods: " + name);
        }
        this.fields = annotation.fields();
    }

    private int countRows(String value) {
        //Long id = value.getId();
        System.out.println("id " + value);

        for (String field : fields) {
            System.err.println("fields " + field);
        }


        //ObjectInputStream d = new ObjectInputStream();

        return 0;
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return countRows(value) == 0;
    }
}